"use strict"

import utils from  './libs/utils'


/**格式化时间
 *  @param {string} time 需要格式化的时间
 *  @param {bool} friendly 是否是fromNow
 */
exports.getLastTimeStr = (time, friendly,format) => {
    if (friendly) {
        return utils.MillisecondToDate(new Date() - new Date(time));
    }else if(format) {
        return utils.fmtDate(new Date(time),format);
    }else{
        return utils.fmtDate(new Date(time),'yyyy-MM-dd');
    }
}

//获取产品状态
exports.getProductStatus = (value) => {
    let arr = ["新建","审批中",'审批被拒绝',"已审批","募集中","封闭中","开放中","已结束","异常结束","取消"];       
    return arr[value];
}

// 获取产品类型
exports.getProductType = (value) => {
    let arr = ['--',"股票型",'债券型',"管理期货","量化对冲","宏观对冲","组合基金","时间驱动","海外基金","复合策略"];      
    return arr[value];
}
//百分比格式
exports.formatPercent = (num,withColor) =>{
    if(withColor)
        return parseFloat(num*100).toFixed(2) + '%';
    var color = num >= 0 ?  'color-danger' : 'color-success';
    return '<span class="' + color + '">' + parseFloat(num*100).toFixed(2) + '%' + '</span>';
}
//保留fixed位小数
exports.formatNumber = (num,fixed) =>{
    return parseFloat(num).toFixed(fixed) ;
}
exports.formatMoney = (s,n)=>{
        //修复负数格式化错误
        var isZ=parseFloat(s)>=0;
        n = n > 0 && n <= 20 ? n : 2;
        s=isZ ? s :s*(-1);
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
        var t = "";
        for(let i = 0; i < l.length; i ++ ){
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
        }
        return isZ ? (t.split("").reverse().join("") + "." + r) : "-"+(t.split("").reverse().join("") + "." + r);
}
exports.orderType = (t)=>{
    var arr = ['','购买','追加','赎回'];
    return arr[t];
}
exports.orderStatus = (s)=>{
    var arr = ["-", "待确认", "未付款", "待复核", "已取消", " 交易成功", "交易失败"];
    return arr[s];
}
exports.hideNumber = (num,start,end)=>{
    if(!num)return;
    var stars='';
    for(var i=0;i<end;i++){
        stars+='*';
    }
    return num.replace(num.substr(start,end),stars); 
}
exports.buyStatus = (s)=>{
    var arr = ["开放购买", "开放购买中"];
    return arr[s];
}
exports.randomStatus = (s)=>{
    var arr = ["开放赎回", "开放赎回中"];
    return arr[s];
}